package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.dto.SuppliersDto;
import com.huanghuai.retail.entity.Factory;
import com.huanghuai.retail.entity.PBrand;
import com.huanghuai.retail.entity.Suppliers;
import com.huanghuai.retail.mapper.SuppliersMapper;
import com.huanghuai.retail.service.BrandService;
import com.huanghuai.retail.service.FactoryService;
import com.huanghuai.retail.service.SuppliersService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers> implements SuppliersService {

    @Autowired
    private SuppliersService suppliersService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private FactoryService factoryService;


    @Override
    public List<Long> getIdsByName(String name) {
        List<Long> idList = new ArrayList<>();
        LambdaQueryWrapper<Suppliers> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Suppliers::getName, name);
        lambdaQueryWrapper.orderByAsc(Suppliers::getId);
        List<Suppliers> suppliersList = suppliersService.list(lambdaQueryWrapper);

        for(int i = 0; i < suppliersList.size(); i ++)
            idList.add(suppliersList.get(i).getBrandId());

        return idList;
    }


    //品牌dto数据处理
    @Override
    public SuppliersDto solveDto(Suppliers suppliers) {

        SuppliersDto suppliersDto = new SuppliersDto();
        BeanUtils.copyProperties(suppliers,suppliersDto);
        //获取厂家信息
        Long factoryId = suppliers.getFactoryId();
        LambdaQueryWrapper<Factory> factoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        factoryLambdaQueryWrapper.eq(Factory::getId,factoryId);
        Factory factory = factoryService.getOne(factoryLambdaQueryWrapper);
        if(factory!=null)
        suppliersDto.setFactoryName(factory.getName());

        //获取品牌信息
        Long brandId = suppliers.getBrandId();
        LambdaQueryWrapper<PBrand> brandLambdaQueryWrapper = new LambdaQueryWrapper<>();
        brandLambdaQueryWrapper.eq(PBrand::getId,brandId);
        PBrand brand = brandService.getOne(brandLambdaQueryWrapper);
        if(brand!=null)
        suppliersDto.setBrandName(brand.getName());
        return suppliersDto;
    }







}
